package com.example.myapplication.ui.fragment;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.myapplication.R;
import com.example.myapplication.bean.ImageBean;
import com.example.myapplication.bean.ImageDetailsBean;
import com.example.myapplication.database.table.ImageDatabase;
import com.example.myapplication.util.ContentUtils;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.Serializable;

/**
 * A simple {@link Fragment} subclass.
 */
public class ItemImageFragment extends BaseFragment {
    ImageDatabase database;
    SQLiteDatabase db;
    ImageView picIv;
    TextView titleTv,painterTv,summaryTv,authorTv;
    Context context;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_item_image, container, false);
        context=getContext();
        initView(view);
        Bundle bundle = getArguments();
        ImageBean.ResultBean bean = (ImageBean.ResultBean) bundle.getSerializable("image");

//        调用访问网络数据的函数
        if (isNetworkConnected(context)) {
            //当网络可用时
            loadData(getContext(), ContentUtils.PICTURE_DETATLS_URL + bean.getId(), ContentUtils.FILE_IMAGE, false, false, true);

        }
        else{
            //当网络不可用时不能执行上面那步，否则报空指针异常
            loadDBData(bean);}
        return view;
    }
    /* 初始化控件 */
    private void initView(View view) {
        picIv = view.findViewById(R.id.itemimage_iv);
        titleTv = view.findViewById(R.id.itemimage_tv_title);
        painterTv = view.findViewById(R.id.itemimage_tv_painter);
        summaryTv = view.findViewById(R.id.itemimage_tv_summary);
        authorTv = view.findViewById(R.id.itemimage_tv_author);
    }
    /*
    判断网络是否可用，
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /* 显示网络数据的方法 */
    @Override
    public void showDataFromNet(String s) {
//        Gson gson = new Gson();
//        ImageDetailsBean bean = gson.fromJson(s, ImageDetailsBean.class);

        //        加载数据
//        String imageURL = bean.getImage1();
//        if (!TextUtils.isEmpty(imageURL)) {
//            imageURL = ContentUtils.PICTURE_DISPLAY_URL+imageURL;
//            Picasso.get().load(imageURL).into(picIv);
//        }
//        titleTv.setText(bean.getTitle());
//        summaryTv.setText(bean.getText1());
//        painterTv.setText(bean.getAuthorbrief());
//        authorTv.setText(bean.getText2());
//    }
    }
    private void loadDBData(ImageBean.ResultBean bean) {
//       1. 获取数据库辅助管理者对象
        database = new ImageDatabase(context, "imagetb.db", null, 1);
//        2.获取数据库对象
        db = database.getWritableDatabase();
        Cursor cursor = db.query("imagetb", null, null, null, null, null, null);

//        读取cursor当中的数据
        while (cursor.moveToNext()) {
            String title = cursor.getString(cursor.getColumnIndex("title"));
            if (bean.getTitle().equals(title)) {
                String pic = cursor.getString(cursor.getColumnIndex("pic"));
                String painter = cursor.getString(cursor.getColumnIndex("painter"));
                String summary = cursor.getString(cursor.getColumnIndex("summary"));
                String author = cursor.getString(cursor.getColumnIndex("author"));
                if (!TextUtils.isEmpty(pic)) {

                    Picasso.get().load(pic).into(picIv);
                }
                titleTv.setText(title);
                summaryTv.setText(summary);
                painterTv.setText(painter);
                authorTv.setText(author);
            }


        }

    }
}
